package cn.cjj.gulimall.order.web;

import cn.cjj.gulimall.order.service.OrderService;
import cn.cjj.gulimall.order.vo.OrderConfirmVo;
import cn.cjj.gulimall.order.vo.OrderSubmitVo;
import cn.cjj.gulimall.order.vo.SubmitOrderResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.OrderComparator;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.concurrent.ExecutionException;

/**
 * @author CuiJunJie
 * @create 2021/7/8-19:24
 */
@Controller
public class PayController {

    @Autowired
    OrderService orderService;


    @RequestMapping("/submitOrder")
    public String submitOrder(OrderSubmitVo order, Model model, RedirectAttributes redirectAttributes) {
        SubmitOrderResponseVo resp = orderService.submitOrder(order);
        System.out.println(resp);
        if (resp.getCode() == 0) {
            model.addAttribute("order", resp.getOrderEntity());
            return "pay";
        } else {
            String msg = "";
            switch (resp.getCode()) {
                case 1:
                    msg = "请刷新页面";
                    break;
                case 2:
                    msg = "价格发生变化";
                    break;
                case 3:
                    msg = "库存不足";
                    break;
                default:
                    msg = "";
            }
            redirectAttributes.addFlashAttribute("msg", msg);
            return "redirect:http://order.gulimall.com/toTrade";
        }
    }


    @GetMapping("/pay.html")
    public String toPayPage() {
        return "pay";
    }


    @RequestMapping("/toTrade")
    public String toTrade(Model model) throws ExecutionException, InterruptedException {
        OrderConfirmVo confirmOrder = orderService.confirmOrder();
        System.out.println(confirmOrder);

        model.addAttribute("confirmOrder", confirmOrder);

        return "confirm";
    }
}
